Wasm: dispatch lifecycle events when async instantiation completes#2232
Wasm: dispatch lifecycle events when async instantiation completes#2232JasonGross wants to merge 5 commits into
Conversation
|
I assume all of the CI failures are spurious? |
b0ea27b to
e5fc28c
Compare
|
re-looking at this, I wonder if we should have a symmetric behavior in the js_of_ocaml side so that it's easier to switch between backend |
2b9ee02 to
cedc3a4
Compare
Done |
Compiling and instantiating the generated WebAssembly is asynchronous, but the launcher only returns a Promise that callers typically don't await. Surrounding JavaScript that runs on DOMContentLoaded therefore has no way to tell when the OCaml exports are actually ready. Dispatch 'wasmoocaml:loaded' (and 'wasmoocaml:error' on failure) CustomEvents on globalThis so external code can wait for the runtime to be ready. Ported from ocaml-wasm/wasm_of_ocaml#143. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The free-variable check in compiler/bin-wasm_of_ocaml/gen/gen.ml flagged the new CustomEvent reference in runtime.js. CustomEvent is a standard DOM/Node global comparable to XMLHttpRequest and DOMException already in this list. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Mirror the Wasm launcher: JavaScript executables now dispatch jsoocaml:loaded / jsoocaml:error CustomEvents on globalThis when their toplevel initialization completes or fails, so surrounding JavaScript can wait for the program the same way with either backend. Separate compilation units, --wrap-with-fun output, build-runtime and build-fs scripts are unaffected. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
The exports expect-test filters top-level statements for jsoo_exports usage; standalone executables are now wrapped in a try/catch dispatching lifecycle events, so teach the filter to look for the program inside that wrapper. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
Cosmetic restructuring of the lifecycle-event application in pack, to keep the Iife branch closer to the formatting conventions used in this file. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
8ea50ee to
1b6238a
Compare
|
CI failure seems like a flake: #=== ERROR while fetching sources for xtmpl.1.2.0 =============================# |
Summary
DOMContentLoadedtherefore has no way to tell when the OCaml exports are actually ready.wasmoocaml:loaded(andwasmoocaml:erroron failure)CustomEvents onglobalThiswhen the asynchronous Wasm instantiation completes, so external code can wait for the runtime to be ready.CustomEventtocompiler/lib/reserved.mlso the free-variable check incompiler/bin-wasm_of_ocaml/gen/gen.mlaccepts the new reference (CustomEventjoins existing standard DOM globals likeXMLHttpRequest/DOMException).Test plan
make lint-jspasses (only an unrelated biome.json schema-version notice)dune build runtime/wasmsucceedsdune build compiler/lib compiler/lib-wasm compiler/bin-wasm_of_ocaml compiler/bin-js_of_ocaml runtimesucceeds (verifies the free-variable check acceptsCustomEvent)make tests— could not run locally; this switch lacksppx_expectand other test-only dependencies. CI should cover it.wasmoocaml:loadedevent fires onglobalThisafter the Wasm runtime finishes initializingwasmoocaml:errorevent fires before the error propagates🤖 Generated with Claude Code